package com.douwong.student.utils;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Created by Tab.lv on 2016/5/9 0009.
 */
public class AppCacheManager {
    /**
     * 保存缓存
     * @param context
     * @param ser  保存的类
     * @param file 保存到该文件夹名
     * @return
     */
    public synchronized static boolean saveObject(Context context, Object ser, String file){
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                oos.close();
            } catch (Exception e) {
            }
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     *
     * @param context
     * @param file
     * @return
     */
    public synchronized static Object readObject(Context context, String file) {
        if (!isExistDataCache(context,file)){
            return null;
        }
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (Serializable) ois.readObject();
        } catch (FileNotFoundException e) {
        } catch (Exception e) {
            e.printStackTrace();
            // 反序列化失败 - 删除缓存文件
            if (e instanceof InvalidClassException) {
                File data = context.getFileStreamPath(file);
                data.delete();
            }
        } finally {
            try {
                ois.close();
            } catch (Exception e) {
            }
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
        return null;
    }

    /**
     * 判断缓存是否存在
     *
     * @param cachefile
     * @return
     */
    public static boolean isExistDataCache(Context context, String cachefile) {
        boolean exist = false;
        File data = context.getFileStreamPath(cachefile);
        if (data.exists())
            exist = true;
        return exist;
    }

    public static void deleteCacheFile(Context context, String cachefile){
        File file=context.getFileStreamPath(cachefile);
        if(file.exists()){
            file.delete();
        }
    }
    public static void deleteCacheFiles(Context context, String... cachefiles){
        for(String cachefile:cachefiles){
            File file=context.getFileStreamPath(cachefile);
            if(file.exists()){
                file.delete();
            }
        }
    }
}
